/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.moderatorplugin;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

import com.pearreview.scribeplugin.ScribeView;
import com.pearreview.ui.FileMenuItem;
import com.pearreview.ui.PearToolBar;
import com.pearreview.ui.TipButton;
import com.pearreview.ui.ToolBarButtonUI;
import com.pearreview.ui.ToolBarSeparator;
import com.pearreview.application.FileHistory;
import com.pearreview.application.MenuBarAdapter;
import com.pearreview.application.Messages;
import com.pearreview.application.Plugin;
import com.pearreview.application.PluginAdapter;
import com.pearreview.application.SaveData;
import com.pearreview.application.FileHistory.ChangeEvent;

public class ModeratorPlugin implements Plugin {
	
	private static final long serialVersionUID = -139945931632011513L;
	
	private PluginAdapter pluginAdapter;
	private ModeratorView view;
	
	/**
	 * 0: normal start (default)<br>
	 * 1: creates new review on start <br>
	 * 2: opens review file on start
	 * 3: load save data
	 */
	private short startMode = 0;
	/**
	 * file to open on start
	 */
	private File fileToOpen;
	/**
	 * save data to load at start
	 */
	private SaveData saveData;
	
	@Override
	public boolean close() {
		return view.close();
	}
	
	public JToolBar getToolBar() {
		
		//---- tool bar ----
		JToolBar toolBar = new PearToolBar();
		
		toolBar.setFloatable(false);
		toolBar.setBackground(new Color(0xd6, 0xd6, 0xd6));
		
		toolBar.setBorder(BorderFactory.createMatteBorder(
				0, 0, 1, 0, new Color(0x828790)));
		
		//---- backBtn ----
		JButton backBtn = new JButton();
		backBtn.setAction(view.getPreviousViewAction());
		Icon ico_prev = getResourceIcon("prev_toolbar_btn.png");
		Icon ico_prev_hover = getResourceIcon("prev_hover_toolbar_btn.png");
		backBtn.setIcon(ico_prev);
		backBtn.setUI(new ToolBarButtonUI(ico_prev_hover));
		backBtn.setToolTipText(
				Messages.getString("PearReview.toolbar.previous"));
		backBtn.setOpaque(false);
		//backBtn.setBackground(new Color(0xd6d6d6));
		
		//---- toStartBtn ----
		JButton toStartBtn = new JButton();
		toStartBtn.setAction(view.getFirstViewAction());
		Icon ico_home = getResourceIcon("home_toolbar_btn.png");
		Icon ico_home_hover = getResourceIcon("home_hover_toolbar_btn.png");
		toStartBtn.setIcon(ico_home);
		toStartBtn.setUI(new ToolBarButtonUI(ico_home_hover));
		toStartBtn.setToolTipText(
				Messages.getString("PearReview.toolbar.start"));
		toStartBtn.setOpaque(false);
		//toStartBtn.setBackground(new Color(0xd6d6d6));
		
		//---- forwardBtn ----
		JButton forwardBtn = new JButton();
		forwardBtn.setAction(view.getNextViewAction());
		Icon ico_next = getResourceIcon("next_toolbar_btn.png");
		Icon ico_next_hover = getResourceIcon("next_hover_toolbar_btn.png");
		forwardBtn.setIcon(ico_next);
		forwardBtn.setUI(new ToolBarButtonUI(ico_next_hover));
		forwardBtn.setToolTipText(
				Messages.getString("PearReview.toolbar.next"));
		forwardBtn.setOpaque(false);
		//forwardBtn.setBackground(new Color(0xd6d6d6));
		
		//---- catalogManager ----
		JButton cmBtn = new JButton();
		cmBtn.setAction(view.getOpenCatalogManagerAction());
		Icon ico_cm = getResourceIcon("view-tree_btn.png");
		Icon ico_cm_hover = getResourceIcon("view-tree_hover_btn.png");
		cmBtn.setIcon(ico_cm);
		cmBtn.setUI(new ToolBarButtonUI(ico_cm_hover));
		cmBtn.setToolTipText(Messages.getString(
				"PearReview.Moderator.aspects.open_manager"));
		cmBtn.setOpaque(false);
		//cmBtn.setBackground(new Color(0xd6d6d6));
		
		//---- fullscreenBtn ----
		JButton fullscreenBtn = new JButton();
		fullscreenBtn.setAction(new FullScreenAction());
		Icon ico_fullscreenBtn = getResourceIcon("fullscreen_hover.png");
		Icon ico_fullscreenBtn_hover = getResourceIcon("fullscreen.png");
		fullscreenBtn.setIcon(ico_fullscreenBtn);
		fullscreenBtn.setUI(new ToolBarButtonUI(ico_fullscreenBtn_hover));
		fullscreenBtn.setToolTipText(
				Messages.getString("PearReview.menu.view.fullscreen"));
		fullscreenBtn.setOpaque(false);
		//fullscreenBtn.setBackground(new Color(0xd6d6d6));
		
		//---- homeTabBtn ----
		JButton homeTabBtn = new JButton();
		homeTabBtn.setAction(view.getNavigateToHomeAction());
		Icon ico_homeTabBtn = getResourceIcon("tab-home.png");
		Icon ico_homeTabBtn_hover = getResourceIcon("tab-home_hover.png");
		homeTabBtn.setIcon(ico_homeTabBtn);
		homeTabBtn.setUI(new ToolBarButtonUI(ico_homeTabBtn_hover));
		homeTabBtn.setToolTipText(
				Messages.getString("PearReview.Moderator.menu.home"));
		homeTabBtn.setOpaque(false);
		//homeTabBtn.setBackground(new Color(0xd6d6d6));
		
		//---- newTabBtn ----
		JButton newTabBtn = new JButton();
		newTabBtn.setAction(new NewTabAction());
		Icon ico_newTabBtn = getResourceIcon("tab-new.png");
		Icon ico_newTabBtn_hover = getResourceIcon("tab-new_hover.png");
		newTabBtn.setIcon(ico_newTabBtn);
		newTabBtn.setUI(new ToolBarButtonUI(ico_newTabBtn_hover));
		newTabBtn.setToolTipText(
				Messages.getString("PearReview.menu.file.new_tab"));
		newTabBtn.setOpaque(false);
		//newTabBtn.setBackground(new Color(0xd6d6d6));
		
		//---- saveBtn ----
		JButton saveTabBtn = new JButton();
		saveTabBtn.setAction(view.getSaveReviewAction());
		Icon ico_saveBtn = getResourceIcon("save_btn.png");
		Icon ico_saveBtn_hover = getResourceIcon("save_hover_btn.png");
		saveTabBtn.setIcon(ico_saveBtn);
		saveTabBtn.setUI(new ToolBarButtonUI(ico_saveBtn_hover));
		saveTabBtn.setToolTipText(
				Messages.getString("PearReview.Moderator.menu.save"));
		saveTabBtn.setOpaque(false);
		//saveTabBtn.setBackground(new Color(0xd6d6d6));
		
		
		//---- newBtn ----
		JButton newBtn = new JButton();
		newBtn.setAction(view.getNewReviewAction());
		Icon ico_newBtn = getResourceIcon("new_hover_btn.png");
		Icon ico_newBtn_hover = getResourceIcon("new_btn.png");
		newBtn.setIcon(ico_newBtn);
		newBtn.setUI(new ToolBarButtonUI(ico_newBtn_hover));
		newBtn.setToolTipText(
				Messages.getString("PearReview.Moderator.menu.new"));
		newBtn.setOpaque(false);
		//newBtn.setBackground(new Color(0xd6d6d6));
		
		
		//---- helpBtn ----
		TipButton helpBtn = new TipButton();
		helpBtn.setFocusable(false);
		
		toolBar.setPreferredSize(new Dimension(Short.MAX_VALUE, 36));
		
		toolBar.add(backBtn);
		toolBar.add(toStartBtn);
		toolBar.add(forwardBtn);
		
		toolBar.add(new ToolBarSeparator(55));
		toolBar.add(newBtn);
		toolBar.add(saveTabBtn);
		
		toolBar.add(homeTabBtn);
		toolBar.add(newTabBtn);
		
		toolBar.add(cmBtn);
		toolBar.add(fullscreenBtn);
		
		toolBar.add(Box.createHorizontalGlue());
		toolBar.add(helpBtn);
		
		view.setTipBtn(helpBtn);
		
		return toolBar;
	}

	@Override
	public void run(PluginAdapter pa) {
		pluginAdapter = pa;
		view = new ModeratorView(pa);
		
		pa.setTabIcon(getResourceIcon("tab-moderator.png"));
		pa.setComponent(view.getPanel());
		pa.setToolBar(getToolBar());
		
		initMenuBar(pluginAdapter.getMenuBarAdapter(), 
				pluginAdapter.getFileHistory());
		
		switch(startMode) {
		case 1: 
			view.newReview();
			break;
		case 2:
			view.openReview(fileToOpen);
			break;
		case 3:
			view.loadSaveData(saveData);
			break;
		}
	}
	
	private void initMenuBar(final MenuBarAdapter mba, final FileHistory fh) {
		/* file */
		JMenuItem item = new JMenuItem(view.getNavigateToHomeAction());
		item.setIcon(getResourceIcon("home_16.png"));
		mba.addFileMenuItem(item);
		mba.addFileMenuItem(null);
		
		item = new JMenuItem(view.getNewReviewAction());
		item.setIcon(getResourceIcon("new_16.png"));
		mba.addFileMenuItem(item);
		
		item = new JMenuItem(view.getOpenReviewAction());
		mba.addFileMenuItem(item);
		
		item = new JMenuItem(view.getSaveReviewAction());
		item.setIcon(getResourceIcon("save_16.png"));
		mba.addFileMenuItem(item);
		
		item = new JMenuItem(view.getSaveAsReviewAction());
		mba.addFileMenuItem(item);
		
		/* catalog manager */
		JMenu menu = new JMenu(
				Messages.getString("PearReview.Moderator.menu.aspects"));
		menu.setFont(new Font("Tahoma", Font.PLAIN, 12));
		item = new JMenuItem(view.getOpenCatalogManagerAction());
		menu.add(item);
		mba.addTopMenu(menu, MenuBarAdapter.AFTER_FILE_MENU);
		
		/* file history */
		fh.addChangeListener(new FileHistory.ChangeListener() {
			private static final long serialVersionUID = 8948507847266853282L;
			@Override
			public void typeChanged(ChangeEvent ce) {
				if(ModeratorView.FILE_HISTORY_TYPE.equals(ce.getType())) {
					mba.setFileHistoryMenuItems(createFileHistoryItems(fh));
				}
			}
		});
		mba.setFileHistoryMenuItems(createFileHistoryItems(fh));
	}
	
	protected List<JMenuItem> createFileHistoryItems(FileHistory fh) {
		List<File> files = fh.getFiles(ScribeView.FILE_HISTORY_TYPE);
		List<JMenuItem> items = new LinkedList<JMenuItem>();
		
		ActionListener al = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FileMenuItem item = (FileMenuItem) e.getSource();
				view.openReview(item.getFile());
			}
		};
		
		int min = Math.min(9, files.size());
		for(int i = 0 ; i < min ; i++) {
			final File file = files.get(i);
			final JMenuItem item = new FileMenuItem(file, 0x31 + i);
			item.addActionListener(al);
			items.add(item);
		}
		
		return items;
	}
	
	public void newReviewOnRun() {
		startMode = 1;
		fileToOpen = null;
		saveData = null;
	}
	
	public void openReviewOnRun(File f) {
		startMode = 2;
		fileToOpen = f;
		saveData = null;
	}
	
	@Override
	public void loadSaveDataOnRun(SaveData data) {
		startMode = 3;
		fileToOpen = null;
		saveData = data;
	}
	
	public PluginAdapter getPluginAdapter() {
		return pluginAdapter;
	}

	@Override
	public String getTitle() {
		return Messages.getString("PearReview.Moderator.title");
	}
	
	/**
	 * Returns a resource icon
	 * @param name name of icon
	 * @return
	 */
	private ImageIcon getResourceIcon(String name) {
		return new ImageIcon(getClass().getResource("images/" + name));
	}

	@Override
	public SaveData getSaveData() {
		return view.getSaveData();
	}
	
	private class FullScreenAction extends AbstractAction {
		private static final long serialVersionUID = -7799874557408926287L;
		@Override
		public void actionPerformed(ActionEvent e) {
			pluginAdapter.setFullScreen(!pluginAdapter.isFullScreen());
		}
		
	}
	
	private class NewTabAction extends AbstractAction {
		private static final long serialVersionUID = -1677609066991420893L;
		@Override
		public void actionPerformed(ActionEvent e) {
			pluginAdapter.newStartTab(true);
		}
	}
}
